#-*- coding:utf-8 -*-
from django.shortcuts import render_to_response
from problem.models import Problem,Relation
from django.http import HttpResponse, HttpResponseRedirect
from datetime import datetime
from Graduation_web.settings import PROBLEM_PER_PAGE_LINES,PROBLEM_BTN_VALUE,PROBLEM_TITLE_VALUE,PROBLEM_BODY_VALUE
from userinfo.models import Userinfo
from Graduation_web.views import get_msg_count
from django.contrib.auth.decorators import login_required

def problem_action(request):
    sign = 'problem'
    perpage = PROBLEM_PER_PAGE_LINES
    username = request.session.get('user','')
    msg_count = get_msg_count(username)
    type = request.GET.get('type','')
    page = request.GET.get('page','1')
    page = int(page)
    if type == 'next':
        page = page + 1
    if type == 'previous':
        page = page - 1
    objs = Problem.objects.all()
    pages = len(objs)/perpage + int(len(objs)%perpage > 0)
    if page > pages:
        page = pages
    if page == 0:
        page = 1
    url = 'problem'
    if len(objs) > 0:
        objs = objs[(page-1)*perpage:page*perpage]
    else:
        empty = True
    btn = PROBLEM_BTN_VALUE
    title = PROBLEM_TITLE_VALUE
    body = PROBLEM_BODY_VALUE
    new_link = '/newproblem'
    link = '/singleproblem'
    return render_to_response('itemlist.html',locals())

def problem_new_action(request):
    sign = 'problem'
    username = request.session.get('user','')
    msg_count = get_msg_count(username)
    action = "add_problem"
    return render_to_response('add_new.html',locals())

@login_required
def add_problem_action(request):
    sign = 'problem'
    username = request.session.get('user','')
    msg_count = get_msg_count(username)
    user_instance = Userinfo.objects.get(username = username)
    if request.method == "POST":
        t_title = request.POST.get("title","")
        t_body = request.POST.get("body","")
        if t_title.strip() != "" and t_body.strip() != "":
            problem = Problem(username = user_instance,title = t_title,body = t_body,date=datetime.now())
            problem.save()
            return HttpResponseRedirect("/problem")
        else:
            error = "不能有空白项"
            action = "add_problem"
            return render_to_response('add_new.html',locals())
    else:
        action = "add_problem"
        return render_to_response('add_new.html',locals())
        
def single_problem_action(request):
    sign = 'problem'
    username = request.session.get('user','')
    msg_count = get_msg_count(username)
    if request.method == "GET":
        type = request.GET.get('type','')
        page = request.GET.get('page','1')
        page = int(page)
        if type == 'next':
            page = page + 1
        if type == 'previous':
            page = page - 1
        perpage = 5
        t_id = request.GET.get("id")
        tolow = request.GET.get("tolow",'')
        try:
            obj = Problem.objects.get(id = int(t_id))
        except:
            erro_msg = '出错了，返回再试试吧！'
            return render_to_response('erro.html',locals())
        relations = Relation.objects.filter(title_id = t_id)
        pages = 1
        if len(relations) > 0:
            pages = len(relations)/perpage + int(len(relations)%perpage > 0)
            if page > pages:
                page = pages
            relations = relations[(page-1)*perpage:page*perpage]
        else:
            empty = True
            page = 1
        url = 'singleproblem'
        return render_to_response('single.html',locals())
